export const getTicks = function(scale) {
  const { min, values, tickCount } = scale;
  let max = Math.max(...values);
  let avg = (max - min) / tickCount;
  avg = Math.ceil(avg);
  const digit = Math.pow(10, avg.toString().length - 1);
  avg = Math.ceil(avg / digit) * digit;
  if (max === 0 && min === 0) {
    scale.max = 1;
    return [0, 1];
  }
  if (max < -Number.MAX_VALUE) {
    return [''];
  } else {
    max = avg * tickCount;
  }
  const ticks = [];
  const toFixedNumber = avg.toString().split('.')[1] ? avg.toString().split('.')[1].length : 0;
  for (let i = min; i < max; i += avg) {
    ticks.push(i.toFixed(toFixedNumber));
  }
  ticks.push(max.toFixed(toFixedNumber));
  scale.max = max;
  return ticks;
};

export const dotTicks = function(scale) {
  const { min, values, tickCount } = scale;
  let max = Math.max(...values);
  let avg = (max - min) / tickCount;
  avg = Math.ceil(avg);
  const digit = Math.pow(10, avg.toString().length - 1);
  avg = Math.ceil(avg / digit) * digit;
  if (max > 2 && max < 5) {
    avg = 1;
  } else if (max >= 1 && max <= 2) {
    avg = 0.5;
  } else if (max >= 0.5 && max < 1) {
    avg = 0.2;
  } else if (max >= 0.2 && max < 0.5) {
    avg = 0.1;
  } else if (max >= 0.08 && max < 0.2) {
    avg = 0.05;
  } else if (max >= 0.04 && max < 0.08) {
    avg = 0.02;
  } else if (max < 0.04) {
    avg = 0.01;
  }
  if (max === 0 && min === 0) {
    scale.max = 1;
    return [0, 1];
  }
  if (max < -Number.MAX_VALUE) {
    return [''];
  } else {
    max = avg * tickCount;
  }
  const ticks = [];
  const toFixedNumber = avg.toString().split('.')[1] ? avg.toString().split('.')[1].length : 0;
  for (let i = min; i < max; i += avg) {
    ticks.push(i.toFixed(toFixedNumber));
  }
  ticks.push(max.toFixed(toFixedNumber));
  scale.max = max;
  return ticks;
};
